package com.xjgzinfo.usdp.web.rest.utils;

import java.io.Serializable;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 补充map排序
 */
public class MapUtils {

    /**
     * 降序
     *
     * @param <K>
     * @param <V> {@Link java.util.Map.comparingByValue}
     * @return
     */
    public static <K, V extends Comparable<? super V>> Comparator<Map.Entry<K, V>> comparingByValueDesc() {
        return (Comparator<Map.Entry<K, V>> & Serializable)
                (c1, c2) -> c2.getValue().compareTo(c1.getValue());
    }

    public static void main(String[] args) {

        Map<String, Double> map = new HashMap<>();
        for (int i = 0; i < 10; i++) {
            double v = Math.random() * 10;
            map.put("a" + i, v);
        }
        List<Map.Entry<String, Double>> list = map.entrySet().stream().sorted(MapUtils.comparingByValueDesc()).collect(Collectors.toList());
        // map转换成list进行排序
        System.out.println(list);
    }
}
